﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNS.Class;

namespace QLNS.NghiepVu
{
    public partial class frmCTKhenThuongKyLuat : Form
    {
        public frmCTKhenThuongKyLuat()
        {
            InitializeComponent();
        }

        DB Cn = new DB();
        string tag = "";


        private void load_dtgNhanVien()
        {
            if (Cn.KetNoi())
            {
                DataTable tbl = Cn.getTable("select row_number() over(order by MaNV) as STT, MaNV, TenNV from tbl_NhanVien");
                dtgNhanVien.DataSource = tbl;
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void frmKhenThuongKyLuat_Load(object sender, EventArgs e)
        {
            try
            {
                if (Cn.KetNoi())
                {
                    cbQD.DisplayMember = "TenLoai";
                    cbQD.ValueMember = "MaLoai";
                    cbQD.DataSource = Cn.getTable("select * from tbl_KhenThuongKyLuat");

                    load_dtgNhanVien();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void load_dtgThuongPhat()
        {
            if (Cn.KetNoi())
            {
                DataTable tbl = Cn.getTable("select * from tbl_CTKhenThuongKyLuat where MaNV='"+dtgNhanVien.CurrentRow.Cells["MaNV"].Value.ToString()+"'");
                dtgThuongPhat.DataSource = tbl;
            }
        }
        private void dtgNhanVien_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgNhanVien.RowCount > 0)
            {
                load_dtgThuongPhat();
            }
        }

        private void dtgThuongPhat_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgThuongPhat.RowCount > 0)
            {
                txtSoQD.Text = dtgThuongPhat.CurrentRow.Cells["SoQD"].Value.ToString();
                dtpNgayQD.Value = DateTime.Parse(dtgThuongPhat.CurrentRow.Cells["NgayQD"].Value.ToString());
                cbQD.SelectedValue = dtgThuongPhat.CurrentRow.Cells["MaLoai"].Value.ToString();
                txtNoiDung.Text = dtgThuongPhat.CurrentRow.Cells["NoiDung"].Value.ToString();
                txtHinhThuc.Text = dtgThuongPhat.CurrentRow.Cells["HinhThuc"].Value.ToString();
                txtMucThuongPhat.Text = dtgThuongPhat.CurrentRow.Cells["MucThuongPhat"].Value.ToString();
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            tag = "them";
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            tag = "sua";
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (Cn.KetNoi())
            {
                if (tag == "them")
                {
                    Cn.Insert_Table("tbl_CTKhenThuongKyLuat", "SoQD,NgayQD,MaNV,MaLoai,NoiDung,HinhThuc,MucThuongPhat", "'" + txtSoQD.Text + "','" + dtpNgayQD.Value.ToString("yyyy-MM-dd") + "','" + dtgNhanVien.CurrentRow.Cells["MaNV"].Value.ToString() + "','" + cbQD.SelectedValue.ToString() + "',N'" + txtNoiDung.Text + "',N'" + txtHinhThuc.Text + "','" + txtMucThuongPhat.Text + "'");
                }
                else
                {
                    Cn.Update_Table("tbl_CTKhenThuongKyLuat","NgayQD='"+dtpNgayQD.Value.ToString("yyyy-MM-dd")+"',MaLoai='"+cbQD.SelectedValue.ToString()+"',NoiDung=N'"+txtNoiDung.Text+"',HinhThuc=N'"+txtHinhThuc.Text+"',MucThuongPhat='"+txtMucThuongPhat.Text+"'","SoQD='"+txtSoQD.Text+"'");
                }
                
            }
            load_dtgThuongPhat();
        }

    }
}
